package com.manli.timing.utils;

import com.alibaba.fastjson.JSONObject;
import com.manli.common.utils.HttpUtil;
import com.manli.timing.dao.TaobaokeOrdersDao;
import com.manli.timing.dto.TaobaokeOrders;
import com.manli.timing.pojo.TklOrderQuery;
import com.manli.timing.pojo.TklOrderQueryReturn;
import com.manli.timing.service.TimingService;
import org.beetl.sql.core.SQLManager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cglib.beans.BeanMap;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Component
public class DingDanXiaUtils {
    private static String orderQueryUrl = "http://api.tbk.dingdanxia.com/tbk/order_details";
    @Autowired
    TimingService timingService;
    @Autowired
    private SQLManager sqlManager;
    @Autowired
    TaobaokeOrdersDao taobaokeOrdersDao;

    /**
     * 淘宝联盟订单查询api接口,淘宝客订单查询api接口
     * @return
     */
    public void queryOrdes(TklOrderQuery t)throws Exception{
        Map map = BeanMap.create(t);
        String result = HttpUtil.httpPost(orderQueryUrl,map);
        JSONObject resultJson = JSONObject.parseObject(result);
        if(200 == resultJson.getInteger("code").intValue()){
            if(resultJson.get("data") != null){
                if(resultJson.getJSONArray("data").size() > 0){
                    for(int i=0;i<resultJson.getJSONArray("data").size();i++){
                        JSONObject object = (JSONObject)resultJson.getJSONArray("data").get(i);
                        TklOrderQueryReturn tklToHelpersReturn = JSONObject.parseObject(object.toJSONString(),TklOrderQueryReturn.class);
                        List<TaobaokeOrders> tklOrderQueryReturns = taobaokeOrdersDao.execute("select * from taobaoke_orders where trade_parent_id = ?",tklToHelpersReturn.getTradeParentId());
                        if(tklOrderQueryReturns.size() > 0){
                            continue;
                        }else{
                            //插入数据库
                            TaobaokeOrders taobaokeOrders = new TaobaokeOrders();
                            taobaokeOrders.setPayPrice(tklToHelpersReturn.getPayPrice());
                            taobaokeOrders.setPubShareFee(tklToHelpersReturn.getPubShareFee());
                            taobaokeOrders.setTradeId(tklToHelpersReturn.getTradeId());
                            taobaokeOrders.setTkOrderRole(tklToHelpersReturn.getTkOrderRole());
                            taobaokeOrders.setTkEarningTime(tklToHelpersReturn.getTkEarningTime());
                            taobaokeOrders.setAdzoneId(tklToHelpersReturn.getAdzoneId());
                            taobaokeOrders.setPubShareRate(tklToHelpersReturn.getPubShareRate());
                            taobaokeOrders.setPubId(tklToHelpersReturn.getPubId());
                            taobaokeOrders.setAlipayTotalPrice(tklToHelpersReturn.getAlipayTotalPrice());
                            taobaokeOrders.setItemTitle(tklToHelpersReturn.getItemTitle());
                            taobaokeOrders.setItemNum(tklToHelpersReturn.getItemNum());
                            taobaokeOrders.setTradeParentId(tklToHelpersReturn.getTradeParentId());
                            taobaokeOrders.setOrderType(tklToHelpersReturn.getOrderType());
                            taobaokeOrders.setTkCreateTime(tklToHelpersReturn.getTkCreateTime());
                            taobaokeOrders.setTkStatus(tklToHelpersReturn.getTkStatus());
                            taobaokeOrders.setItemPrice(tklToHelpersReturn.getItemPrice());
                            taobaokeOrders.setTotalCommissionFee(tklToHelpersReturn.getTotalCommissionFee());
                            taobaokeOrders.setIncomeRate(tklToHelpersReturn.getIncomeRate());
                            taobaokeOrders.setTotalCommissionRate(tklToHelpersReturn.getTotalCommission_rate());
                            taobaokeOrders.setSpecialId(tklToHelpersReturn.getSpecialId());
                            taobaokeOrders.setRelationId(tklToHelpersReturn.getRelationId());
                            taobaokeOrders.setQStartTime(t.getStart_time());
                            taobaokeOrders.setQEndTime(t.getEnd_time());
                            taobaokeOrdersDao.insert(taobaokeOrders);
                        }

                    }
                }
            }

            if(resultJson.getBoolean("has_next")){
                Integer page1 = Integer.parseInt(t.getPage_no()) + 1;
                timingService.getTaoBaoKeOrders(page1.toString(), t.getStart_time(), t.getEnd_time());
            }
        }

    }

    /**
     * 更新状态
     * @return
     */
    public void queryOrdesStatus(TklOrderQuery t)throws Exception{
        Map map = BeanMap.create(t);
        String result = HttpUtil.httpPost(orderQueryUrl,map);
        JSONObject resultJson = JSONObject.parseObject(result);
        if(200 == resultJson.getInteger("code").intValue()){
            if(resultJson.get("data") != null){
                if(resultJson.getJSONArray("data").size() > 0){
                    for(int i=0;i<resultJson.getJSONArray("data").size();i++){
                        JSONObject object = (JSONObject)resultJson.getJSONArray("data").get(i);
                        TklOrderQueryReturn tklToHelpersReturn = JSONObject.parseObject(object.toJSONString(),TklOrderQueryReturn.class);
                        List<TaobaokeOrders> tklOrderQueryReturns = taobaokeOrdersDao.execute("select * from taobaoke_orders where tk_status != '3' and trade_parent_id = ?",tklToHelpersReturn.getTradeParentId());
                        if(tklOrderQueryReturns.size() > 0){
                            //改变状态
                            taobaokeOrdersDao.executeUpdate("update taobaoke_orders set tk_status = ? where trade_parent_id = ?",tklToHelpersReturn.getTkStatus(),tklToHelpersReturn.getTradeParentId());
                        }
                    }
                }
            }

            if(resultJson.getBoolean("has_next")){
                Integer page1 = Integer.parseInt(t.getPage_no()) + 1;
                TklOrderQuery tklOrderQuery = new TklOrderQuery(timingService.dingdanxiaKey,t.getStart_time(), t.getEnd_time(),"",page1.toString());
                queryOrdesStatus(tklOrderQuery);
            }
        }

    }

}
